package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.Constants;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.domain.Room;
import com.zzyl.nursing.mapper.RoomMapper;
import com.zzyl.nursing.service.IRoomService;
import com.zzyl.nursing.vo.BedVo;
import com.zzyl.nursing.vo.DeviceInfo;
import com.zzyl.nursing.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 房间Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询房间
     *
     * @param id 房间主键
     * @return 房间
     */
    @Override
    public Room selectRoomById(Long id) {
        return getById(id);
    }

    /**
     * 查询房间列表
     *
     * @param room 房间
     * @return 房间
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        return save(room) ? 1 : 0;
    }

    /**
     * 修改房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        return updateById(room) ? 1 : 0;
    }

    /**
     * 批量删除房间
     *
     * @param ids 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 根据楼层 id 获取房间视图对象列表
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
        return roomMapper.selectByFloorId(floorId);
    }


    /**
     * 获取所有房间（负责老人）
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }

    @Override
    public RoomVo getRoomById(Integer id) {
            return roomMapper.getRoomById(id);
    }

    @Override
    public List<RoomVo> getRoomsWithDeviceByFloorId(Long floorId) {
            //1.查询数据库
            List<RoomVo> vo =roomMapper.getRoomsWithDeviceByFloorId(floorId);
            //2.从redis中获取设备数据
        for (RoomVo roomVo : vo) {//遍历房间
            List<DeviceInfo> deviceVos = roomVo.getDeviceVos();//获取设备数组
            for (DeviceInfo deviceVo : deviceVos) {//遍历设备数组
                String cache = (String) redisTemplate.opsForHash().get(Constants.IOT_DEVICE_LAST_DATA, deviceVo.getIotId());
                if(StrUtil.isNotEmpty(cache)){//这步忘了,一定要加上非空校验
                    List<DeviceData> list = JSONUtil.toList(cache, DeviceData.class);
                    deviceVo.setDeviceDataVos(list);//将从redis中获取的设备信息传递给vo
                }

            }
            List<BedVo> bedVoList = roomVo.getBedVoList();
            for (BedVo bedVo : bedVoList) {
                List<DeviceInfo> deviceVos1 = bedVo.getDeviceVos();
                for (DeviceInfo deviceVo1 : deviceVos1) {//遍历设备数组
                    String cache = (String) redisTemplate.opsForHash().get(Constants.IOT_DEVICE_LAST_DATA, deviceVo1.getIotId());
                    if(StrUtil.isNotEmpty(cache)){//这步忘了,一定要加上非空校验
                        List<DeviceData> list = JSONUtil.toList(cache, DeviceData.class);
                        deviceVo1.setDeviceDataVos(list);//将从redis中获取的设备信息传递给vo
                    }
                }
            }
        }

            return vo;
    }
}
